/**
 * 字符工具
 */
class DateUtil {
	/**
	 * 日期格式化方法
	 * @param {Date} date 日期对象
	 * @param {string} formatStr 格式字符串
	 * @return {string} 格式化后的日期字符串
	 */
	formatDate(date, formatStr) {
		if (!(date instanceof Date)) {
			date = new Date(date);
		}
		const padZero = num => (num < 10 ? `0${num}` : num);
		const tokens = {
			yyyy: date.getFullYear(),
			yy: String(date.getFullYear()).slice(-2),
			MM: padZero(date.getMonth() + 1),
			M: date.getMonth() + 1,
			dd: padZero(date.getDate()),
			d: date.getDate(),
			HH: padZero(date.getHours()),
			H: date.getHours(),
			hh: padZero(date.getHours() % 12 || 12),
			h: date.getHours() % 12 || 12,
			mm: padZero(date.getMinutes()),
			m: date.getMinutes(),
			ss: padZero(date.getSeconds()),
			s: date.getSeconds(),
			SSS: String(date.getMilliseconds()).padStart(3, "0"),
			a: date.getHours() < 12 ? "AM" : "PM",
			A: date.getHours() < 12 ? "am" : "pm",
			E: ["日", "一", "二", "三", "四", "五", "六"][date.getDay()],
			D: ["Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday"][date.getDay()],
			T: "T" // 时区占位符，实际应用中可以根据需要扩展
		};

		return formatStr.replace(/(yyyy|yy|MM|M|dd|d|HH|H|hh|h|mm|m|ss|s|SSS|a|A|E|D|T)/g, match => tokens[match] || match);
	}
}

module.exports = new DateUtil();
